package org.erepublik.company.controllers;

import java.util.ArrayList;

import org.erepublik.company.log.SitesLogger;
import org.erepublik.company.product.House;
import org.erepublik.company.searchCriteria.EAllianceSearchCriteria;
import org.erepublik.company.searchCriteria.EQualityFilter;
import org.erepublik.company.searchCriteria.HouseSearchCriteria;
import org.erepublik.company.service.IProductsService;
import org.erepublik.company.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/houses.html")
public class HousesController extends AbstractController {

    @Autowired // autumatycznie wstrzykiwany obiekt
    private SitesLogger sitesLogger; 
    
    @Autowired // autumatycznie wstrzykiwany obiekt
    private IProductsService productService; 
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
        House[] products = productService.getHouses(null);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceHouseUpdate() );
        model.addAttribute("products", products);
        model.addAttribute("houseSearchCriteria", new HouseSearchCriteria());
        sitesLogger.log("HOUSE", "Country: null MaxWellness: false Quality: null");
        return "houses";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("houseSearchCriteria") HouseSearchCriteria criteria) {
        fillModelMap(model);
        House[] products = productService.getHouses(criteria.getCountry());
        if (criteria.isMaxWellness())
            products = maxWellnessFilter(products);
        if (criteria.getQualityFilter() != EQualityFilter.ALL)
            products = qualityFilter(products, criteria.getQualityFilter());
        products = filterAlliances(criteria.getAlliance(), products);
        products = shrinkTable(products);
        model.addAttribute("updateTime", productService.getMinutesSinceHouseUpdate() );
        model.addAttribute("products", products);
        sitesLogger.log("HOUSE", "Country: "+criteria.getCountry()
                +" MaxWellness: "+criteria.isMaxWellness()
                +" Quality: "+criteria.getQualityFilter());
        return "houses";
    }
    
    private House[] qualityFilter(House[] products, EQualityFilter filter) {
        ArrayList<House> houses = new ArrayList<House>();
        for (House house : products) {
            if (house.getQuality() == filter.getQuality())
                houses.add(house);
        }
        return houses.toArray(new House[0]);
    }

    final int MAX_SIZE=50; // maksymalna ilosc wyswietlanych produktow
    // skraca tabele do MAX_SIZE produktow
    private House[] shrinkTable(House[] products) {
        if (products.length <= MAX_SIZE)
            return products;
        else {
            House[] newTable = new House[MAX_SIZE];
            for (int i=0; i < MAX_SIZE; i++) {
                newTable[i]=products[i];
            }
            return newTable;
        }
    }
    
    private House[] maxWellnessFilter(House[] houses) {
        ArrayList<House> housesList = new ArrayList<House>();
        for (House house : houses) {
            if (house.getWellness()==50.0d)
                housesList.add(house);
        }
        return housesList.toArray(new House[0]);
    }
 
    
    private House[] filterAlliances(EAllianceSearchCriteria alliance, House[] products) {

        ArrayList<House> list = new ArrayList<House>();
        for (House food : products) {
            if ( Utils.contains( food.getCountry().getAlliance(), alliance.getAlliances() ) )
                list.add(food);
        }
        return list.toArray(new House[0]);
    }
    
    
    
}
